package com.filetransfer.view.Components.render;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
public class ProgressBarRenderer extends JProgressBar implements TableCellRenderer {
    public ProgressBarRenderer() {
        setStringPainted(true);
        setOpaque(true);
        setBackground(null);
        setForeground(null);
        setBorderPainted(false);
    }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (value instanceof Integer) {
            setValue((Integer) value);
            int progress = (Integer) value;
            var background = isSelected ? table.getSelectionBackground() : table.getBackground();
            setBackground(background);
            setForeground(background);
            setFont(table.getFont());
            if (progress == 0) {
                setString("-");
            } else if (progress == 100) {
                setString("完成(100%)");
            } else {
                var foreground = isSelected ? table.getSelectionForeground() : table.getForeground();
                setForeground(foreground);
                setString(value + "%");
            }
        }
        return this;
    }
}

